package kr.saeildamil.androidstudy_second20.common;

import kr.saeildamil.androidstudy_second.R;
import android.app.Activity;
import android.app.ListFragment;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class WordListFragment extends ListFragment {
	String[] words;
	OnWordChangedListener mHost;
	
	public interface OnWordChangedListener{
		public void onWordChanged(int index);
	}
	
	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		try{
			mHost = (OnWordChangedListener) activity;
		}catch(ClassCastException e){
			/*activity가 반드시 OnWordChangedListener를 구현해야 됨*/
			throw new ClassCastException("Activity must implement OnWordChangedLister");
		}
		
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		words = getResources().getStringArray(R.array.word);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		ArrayAdapter<CharSequence> adapter = 
				ArrayAdapter.createFromResource(getActivity(), R.array.word, android.R.layout.simple_list_item_1);
		
		setListAdapter(adapter);
		getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
	}
	
	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		mHost.onWordChanged(position);
		/*String message = words[position] + ":" + details[position];
		Toast.makeText(getActivity(), message, Toast.LENGTH_LONG).show();*/
	}
}
